package com.as3.timbot.iso
{
	import com.as3.timbot.command.Command;
	import com.as3.timbot.iso.Actor;
	import com.as3.timbot.command.CommandEvent;
	import com.as3.timbot.iso.Actor;
	
	import flash.events.EventDispatcher;
	import flash.display.MovieClip;

	public class ActorMoveCommand implements Command
	{
		private var __dispatcher:EventDispatcher;
		private var __event:CommandEvent;
		private var __actor:Actor;
		private var __x:Number;
		private var __y:Number;
		private var __sx:Number;
		private var __sy:Number;
		
		public function ActorMoveCommand(actor:Actor,x:Number,y:Number,sx:Number,sy:Number)
		{
			__actor = actor;
			var character:MovieClip = __actor.getGraphic()[0];
			///////////////////////////////////////////////
			/*var x:Number = world.char.x;
			var z:Number = world.char.z;
			world.char.startx = x;
			world.char.startz = z;
			world.char.endx = xm;
			world.char.endz = zm;
			var angleSpan:Number = 360/8;
			var angle:Number = Math.atan2(zm-z, xm-x);
			var realAngle:Number = angle*180/Math.PI;
			realAngle += angleSpan/2;
			if (realAngle<0) {
				realAngle += 360;
			}
			var frame:Number = Math.ceil(realAngle/angleSpan);
			world.char.clip.gotoAndStop("walk"+frame);*/
		///////////////////////////////////////////////////
			__x = x;
			__y = y;
			__sx = sx;
			__sy = sy;
			__dispatcher = new EventDispatcher();
		}
		public function execute():void
		{
			begin();
			__actor.positionActor(__x,__y,__sx,__sy);
			progress(100);
			end();
		}
		//get the actor and move it.........t.w.
		
		public function addEventListener(type:String,callback:Function):void
		{
			__dispatcher.addEventListener(type,callback);
		}
		private function begin():void
		{
			__event = new CommandEvent(CommandEvent.START);
			fireEvent();
		}
		private function end():void
		{
			__event = new CommandEvent(CommandEvent.COMPLETE);
			fireEvent();
		}
		private function progress(value:Number):void
		{
			__event=new CommandEvent(CommandEvent.PROGRESS);
			__event.setProgress(value);
			fireEvent();
		}
		private function fireEvent():void
		{
			__event.setCommand(this);
			__dispatcher.dispatchEvent(__event);
		}
		
	}
}